<?php

/**
 * Created by Reliese Model.
 */

namespace App\Models;

use Carbon\Carbon;

/**
 * Class Order
 * 
 * @property int $id
 * @property string $order_sn
 * @property int $uid
 * @property float $amount
 * @property float $pay_amount
 * @property string|null $address
 * @property string|null $name
 * @property string|null $phone
 * @property float|null $transfer_amount
 * @property int|null $status
 * @property string|null $source
 * @property int|null $source_id
 * @property Carbon|null $pay_time
 * @property Carbon|null $delivery_time
 * @property Carbon|null $receive_time
 * @property int $order_express_id
 * @property string $message
 * @property float $commission_amount
 * @property int $pid
 * @property Carbon|null $create_at
 * @property Carbon|null $update_at
 * @property Carbon|null $delete_at
 *
 * @package App\Models
 */
class Order extends BaseModels
{
	protected $table = 'zh_order';

	const UN_PAY = 1; //待支付

    const DELIVER = 2; //待发货

    const RECEIVING = 3; //待收货

    const SUCCESS = 4; //已完成

    const CANCEL = -1; //已取消

	protected $casts = [
        'uid'             => 'int',
        'amount'          => 'float',
        'pay_amount'      => 'float',
        'transfer_amount' => 'float',
        'status'          => 'int'
	];

	protected $dates = [
		'pay_time',
		'delivery_time',
		'receive_time',
		'create_at',
		'update_at',
		'delete_at'
	];

	protected $fillable = [
		'order_sn',
		'uid',
		'amount',
		'pay_amount',
		'address',
		'name',
		'phone',
		'transfer_amount',
		'status',
		'source',
        'source_id',
		'pay_time',
		'delivery_time',
		'receive_time',
        'order_express_id',
        'message',
        'commission_amount',
        'pid',
		'create_at',
		'update_at',
		'delete_at'
	];


	protected $appends = [
	    'status_string', 'source_string', 'user'
    ];

    /**
     * 订单详情
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
	public function details()
    {
        return $this->hasMany(OrderDetail::class, 'order_id','id');
    }


    /**
     * 物流详情
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function express()
    {
        return $this->hasOne(OrderExpress::class,'order_id','id');
    }

    /**
     * @return string
     */
    public function getStatusStringAttribute()
    {
        switch ($this->status){
            case self::UN_PAY:
                return '未支付';
            case self::DELIVER:
                return '待发货';
            case self::RECEIVING:
                return '待收货';
            case self::SUCCESS:
                return '已完成';
            case self::CANCEL:
                return '已取消';
            default:
                return '未知';
        }
    }


    /**
     * @return string
     */
    public function getSourceStringAttribute()
    {
        switch ($this->source){
            case 'share_free':
                return '分享免费拿';
            default:
                return '商城订单';
        }
    }

    protected function getUserAttribute()
    {
        return User::find($this->uid)->toArray();
    }


}
